#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QHostAddress>
#include<QAbstractSocket>
#include<QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    ,tcp(new QTcpSocket(this))
    ,data(new XProtocol)
{

    data->tcp = tcp;

    ui->setupUi(this);
    tcp->bind(QHostAddress::Any,8888);
    ui->ip->setText("192.168.6.75");
    ui->port->setText("8000");


    connect(tcp,SIGNAL(readyRead()),this,SLOT(responseHandler()));
    connect(tcp,&QTcpSocket::connected,[this](){
        ui->netinfo->setText("已连接");
        ui->netinfo->setStyleSheet("color:green;");
    });

    connect(tcp,SIGNAL(error(QAbstractSocket::SocketError))
            ,this,SLOT(on_error(QAbstractSocket::SocketError)));

    connect(tcp,&QTcpSocket::disconnected,[this](){
        ui->netinfo->setText("已经断开连接");
        ui->netinfo->setStyleSheet("color:red;");
    });
//    connect(this,&MainWindow::destroyed,[this](auto o){
//        qDebug() << "清理资源";
//        if(tcp->isOpen()){
//            tcp->close();
//        }
//    });

    qDebug() << "size i64:" << sizeof(i64);
    qDebug() << "size u64:" << sizeof(u64);
    qDebug() << "size ptr:" << sizeof(ptr);
    qDebug() << "size Protocol:" << sizeof(XProtocol);
    qDebug() << "size Protocol:" << sizeof(XProtocol::XHeader);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::closeEvent(QCloseEvent *e){
    qDebug() << "清理资源";
            if(tcp->isOpen()){
                tcp->close();
            }
}

void MainWindow::on_error(QAbstractSocket::SocketError err){

    ui->netinfo->setText(tcp->errorString());
    ui->netinfo->setStyleSheet("color:red;");
}

void MainWindow::responseHandler(){
    QString info = ui->recvDisplay->toPlainText();


    XProtocol responseData = {};
    qint64 len = tcp->read(reinterpret_cast<char*>(&responseData.header),sizeof(responseData.header));
    if(len < sizeof(responseData.header)){
        info.append("error:数据头部长度错误\n");
        ui->recvDisplay->setPlainText(info);
        tcp->close();
//        data->close();
        return;
    }
    switch (responseData.header.type) {
    case XCLOSE:
        info.append("type close:关闭连接\n");
        tcp->close();
        break;
    case XMSG:
        info.append("收到服务器响应:");
        info.append(data->msg());
        info.append('\n');
        break;
    case XFILE:
        break;
    default:
        break;
    }


   ui->recvDisplay->setPlainText(info);


}

void MainWindow::on_sendMsg_clicked()
{

    QString str = ui->sendContent->toPlainText();
    //就是你个狗日的搞鬼，服务器接收数据不正常
//    tcp->write(str.toUtf8(),str.toUtf8().length());

    data->msg(str);
}

void MainWindow::on_connect_clicked()
{
    tcp->connectToHost(ui->ip->text(),ui->port->text().toInt(nullptr));
}

void MainWindow::on_disconnect_clicked()
{
    data->close();
}

void MainWindow::on_uploadFile_clicked()
{
    QString filepath = QFileDialog::getOpenFileName();
    ui->uploadFilePath->setText(filepath);
    ui->uploadFilePath->setStyleSheet("color:blue;");

    data->uploadFile(filepath);

}
